package com.attendance.www.hr.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.EmployeePunchTimeBean;
import com.attendance.www.hr.beans.SchedulWeekBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.dao.mapper.AttachmentMapper;
import com.attendance.www.hr.dao.mapper.EmployeeExtMapper;
import com.attendance.www.hr.dao.mapper.EmployeeMapper;
import com.attendance.www.hr.dao.mapper.EmployeeSchedulMapper;
import com.attendance.www.hr.dao.mapper.HolidayMapper;
import com.attendance.www.hr.dao.mapper.PunchTimeMapper;
import com.attendance.www.hr.dao.mapper.SchedulDayMapper;
import com.attendance.www.hr.dao.mapper.SchedulWeekExtMapper;
import com.attendance.www.hr.dao.mapper.SchedulWeekMapper;
import com.attendance.www.hr.entitys.Attachment;
import com.attendance.www.hr.entitys.Employee;
import com.attendance.www.hr.entitys.EmployeeSchedul;
import com.attendance.www.hr.entitys.EmployeeSchedulExample;
import com.attendance.www.hr.entitys.Holiday;
import com.attendance.www.hr.entitys.HolidayExample;
import com.attendance.www.hr.entitys.PunchTime;
import com.attendance.www.hr.entitys.PunchTimeExample;
import com.attendance.www.hr.entitys.SchedulDayExample;
import com.attendance.www.hr.entitys.SchedulDayKey;
import com.attendance.www.hr.entitys.SchedulWeek;
import com.attendance.www.hr.entitys.SchedulWeekExample;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IApprovalService;
import com.attendance.www.hr.service.ISchedulWeekService;

@Service
public class SchedulWeekServiceImpl implements ISchedulWeekService {
	
	@Resource
	private SchedulWeekMapper schedulWeekDao;
	@Resource
	private SchedulWeekExtMapper schedulWeekExtDao;
	@Resource
	private SchedulDayMapper schedulDayDao;
	@Resource 
	private HolidayMapper holidayDao;
	@Resource 
	private EmployeeSchedulMapper employeeSchedulDao;
	@Resource 
	private EmployeeMapper employeeDao;
	@Resource 
	private IApprovalService approvalService;
	@Resource
	private PunchTimeMapper punchTimeDao;
	@Resource
	private AttachmentMapper attachmentDao;
	@Resource
	private EmployeeExtMapper employeeExtDao;
	

	private List<String> errorMsgList;//错误信息列表
	
	/**
	 * 新增班次，同时新增排班
	 */
	@Override
	public boolean saveSchedulWeek(SchedulWeekBean schedulWeekBean) {
		
		int record;
		//具体排班表
		SchedulDayKey schedulDayKey=new SchedulDayKey();//班次日表(双主键)
		//班次表
		SchedulWeek schedulWeek=new SchedulWeek();
		int swType=schedulWeekBean.getSwType();
		Date fromDate=schedulWeekBean.getFromDate();
		Date thruDate=schedulWeekBean.getThruDate();
		
		schedulWeek.setSwName(schedulWeekBean.getSwName());
		schedulWeek.setSwType(swType);
		schedulWeek.setFromDate(fromDate);
		schedulWeek.setThruDate(thruDate);
		schedulWeek.setIsHoliday(schedulWeekBean.getIsHoliday());
		
		//判断班次类型
		if (swType == 1) {//普通排班
			//先不做名称重复验证
			int week0 = schedulWeekBean.getSwSunday();
			int week1 = schedulWeekBean.getSwMonday();
			int week2 = schedulWeekBean.getSwTuesday();
			int week3 = schedulWeekBean.getSwWednesday();
			int week4 = schedulWeekBean.getSwThursday();
			int week5 = schedulWeekBean.getSwFriday();
			int week6 = schedulWeekBean.getSwSaturday();
			
			schedulWeek.setSwMonday(schedulWeekBean.getSwMonday());
			schedulWeek.setSwTuesday(schedulWeekBean.getSwTuesday());
			schedulWeek.setSwWednesday(schedulWeekBean.getSwWednesday());
			schedulWeek.setSwThursday(schedulWeekBean.getSwThursday());
			schedulWeek.setSwFriday(schedulWeekBean.getSwFriday());
			schedulWeek.setSwSaturday(schedulWeekBean.getSwSaturday());
			schedulWeek.setSwSunday(schedulWeekBean.getSwSunday());
			schedulWeek.setIsDefault(schedulWeekBean.getIsDefault());
			schedulWeek.setIsHoliday(schedulWeekBean.getIsHoliday());
			
			//新增班次
			record=schedulWeekDao.insertSelective(schedulWeek);
			if (record<=0) {
				return false;
			}
			
			int swId=schedulWeek.getSwId();
			/**
			 * 新增班天表 t_schedul_day
			 */
			//周班次星期几上班与否的list<boolean>
			List<Boolean> weekBooleanList = new ArrayList<>();
			if (week0==1){//星期日，index=0
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			if (week1==1){
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			if (week2==1){
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			if (week3==1){
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			if (week4==1){
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			if (week5==1){
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			if (week6==1){
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			//星期计算
			Calendar calWeek = Calendar.getInstance();  
			calWeek.setTime(fromDate);
			int dayForWeek = calWeek.get(Calendar.DAY_OF_WEEK) - 1;//星期日是0....
			//两个日期循环
			Long startTime = fromDate.getTime();
			Long endTime = thruDate.getTime();
			Long oneDay = 1000 * 60 * 60 * 24l;
			schedulDayKey.setSwId(swId);
			while (startTime <= endTime) {
				int index = dayForWeek % 7;//循环下标的布尔值
				Date sdDate = new Date(startTime);
				if (weekBooleanList.get(index)){
					schedulDayKey.setSdDate(sdDate);
					record = schedulDayDao.insert(schedulDayKey);
					if (record<=0) {
						return false;
					}
				}
				dayForWeek++;
				startTime += oneDay;
			}
			//判断is Holiday
			if (schedulWeekBean.getIsHoliday()==1){
				//查询某年的节假日
				HolidayExample holidayExample=new HolidayExample();
				int year = calWeek.get(Calendar.YEAR);
				//不已开始结束日期取范围，有可能有调休的临界日期
				holidayExample.createCriteria().andHolidayYearEqualTo(year);
				List<Holiday> holidayList = holidayDao.selectByExample(holidayExample);
				//删除再添加，两步走
				SchedulDayKey sdKey=new SchedulDayKey();
				sdKey.setSwId(swId);
				for (Holiday holiday : holidayList){
					Date holidayDate=holiday.getHolidayDate();
					sdKey.setSdDate(holidayDate);
					record=schedulDayDao.deleteByPrimaryKey(sdKey);
					schedulDayKey.setSdDate(holidayDate);
					if (holiday.getIsToWork()==1){
						schedulDayDao.insertSelective(schedulDayKey);
					}
				}
				if (swId != 1) {
					//删除多余的法定调休节日排班
					SchedulDayExample schedulDayExample=new SchedulDayExample();
					schedulDayExample.createCriteria().andSwIdEqualTo(swId)
					.andSdDateNotBetween(fromDate, thruDate);
					schedulDayDao.deleteByExample(schedulDayExample);
				}
			}
		} else if (swType == 2) {//特殊排班
			//新增排班schedulWeek
			record=schedulWeekDao.insertSelective(schedulWeek);
			if (record<=0) {
				return false;
			}
			//新增实际上班日表schedulDay
			int swId=schedulWeek.getSwId();
			schedulDayKey.setSwId(swId);
			List<Date> dateList=schedulWeekBean.getDateList();
			for (Date date : dateList){
				schedulDayKey.setSdDate(date);
				schedulDayDao.insertSelective(schedulDayKey);
			}
		}
	    
		/**
		 * 新增排班--员工-- EmployeeSchedul
		 */
		List<Integer> employeeIdList=schedulWeekBean.getEmployeeIdList();
		//employeeIdList是否为空
		if (employeeIdList.size()>0) {
			
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(schedulWeekBean.getFromDate());
			calendar.add(Calendar.DATE, -1);
			Date thruDateFront = calendar.getTime();
			/**
			 * 原排班的结束日期大于本次排班的开始日期，并且原排班的开始日期小于本次排班的结束日期，就是有交集！
			 */
			//查出默认班次ID，修改时排除默认班次
			SchedulWeekExample schedulWeekExample=new SchedulWeekExample();
			schedulWeekExample.createCriteria().andIsDefaultEqualTo(1);
			List<SchedulWeek> schedulWeekList=schedulWeekDao.selectByExample(schedulWeekExample);
			Integer defaultId=schedulWeekList.get(0).getSwId();
			//更新有交集的排班,设置人员上个排班结束时间,为本次排班的开始日期的前一天,
			EmployeeSchedulExample employeeSchedulExample=new EmployeeSchedulExample();
			employeeSchedulExample.createCriteria().andFromDateLessThanOrEqualTo(thruDate)
			.andThruDateGreaterThanOrEqualTo(fromDate)
			.andEmployeeIdIn(schedulWeekBean.getEmployeeIdList())
			.andSwIdNotEqualTo(defaultId);
			EmployeeSchedul employeeSchedul=new EmployeeSchedul();
			employeeSchedul.setThruDate(thruDateFront);
			employeeSchedulDao.updateByExampleSelective(employeeSchedul, employeeSchedulExample);
			
			//新增人员排班 
			List<EmployeePunchTimeBean> epList=schedulWeekBean.getEmployeePunchTimeList();
			if (epList == null || epList.size() == 0) {//正常情况
				EmployeeSchedul employeeSchedul2=new EmployeeSchedul();
				employeeSchedul2.setSwId(schedulWeek.getSwId());
				employeeSchedul2.setPunchTimeId(schedulWeekBean.getPunchTimeId());
				employeeSchedul2.setFromDate(schedulWeekBean.getFromDate());
				employeeSchedul2.setThruDate(schedulWeekBean.getThruDate());
				for (Integer integer : employeeIdList) {
					employeeSchedul2.setEmployeeId(integer);
					record=employeeSchedulDao.insertSelective(employeeSchedul2);
					if (record<=0) {
						return false;
					}
				}
			} else {//每天不同打卡时间的排班，循环按天增加班次
				for (EmployeePunchTimeBean employeePunchTimeBean : epList) {
					EmployeeSchedul employeeSchedul2=new EmployeeSchedul();
					employeeSchedul2.setSwId(schedulWeek.getSwId());
					employeeSchedul2.setPunchTimeId(employeePunchTimeBean.getPunchTimeId());
					employeeSchedul2.setFromDate(employeePunchTimeBean.getDate());
					employeeSchedul2.setThruDate(employeePunchTimeBean.getDate());
					for (Integer integer : employeeIdList) {
						employeeSchedul2.setEmployeeId(integer);
						if(employeeSchedul2.getPunchTimeId()==null){
							employeeSchedul2.setPunchTimeId(1);
						}
						record=employeeSchedulDao.insertSelective(employeeSchedul2);
						if (record<=0) {
							return false;
						}
					}
				}
			}
		}
		return true;
	}

	/**
	 * 删除班次，同时设置排班结束时间
	 */
	@Override
	public boolean deleteSchedulWeek(String swIdStr) {
		int swId=Integer.parseInt(swIdStr);
		List<Integer> employeeIdList=schedulWeekExtDao.getEmployeeListBySwId(swId);
		//物理删除排班表 t_schedul_week
		SchedulWeek schedulWeek = schedulWeekDao.selectByPrimaryKey(swId);
		
		int record=schedulWeekDao.deleteByPrimaryKey(swId);
		if(record<=0){
			return false;
		}
		//删除班次天表 t_schedul_day信息
		SchedulDayExample schedulDayExample=new SchedulDayExample();
		schedulDayExample.createCriteria().andSwIdEqualTo(swId);
		schedulDayDao.deleteByExample(schedulDayExample);
		
		//删除人员排班表 t_employee_schedul
		if (employeeIdList.size()>0) {
			EmployeeSchedulExample example=new EmployeeSchedulExample();
			example.createCriteria().andSwIdEqualTo(swId);
			employeeSchedulDao.deleteByExample(example);
			//重计算
			Date startDate = schedulWeek.getFromDate();
			Date endDate = schedulWeek.getThruDate();
			Date curDate = new Date();
			if (startDate.after(curDate)) {
				startDate = curDate;
			}
			if (endDate.after(curDate)) {
				endDate = curDate;
			}
			approvalService.resetAttendance(startDate, endDate, employeeIdList);
		}
		return true;
	}

	/**
	 * 修改班次，修改班次名称；员工，修改打卡时间；修改结束时间 只能延长判断
	 */
	@Override
	public boolean updateSchedulWeek(SchedulWeekBean schedulWeekBean) {
		
		int swType=schedulWeekBean.getSwType();
		SchedulWeek schedulWeek=new SchedulWeek();
		SchedulDayKey schedulDayKey=new SchedulDayKey();//班次日表(双主键)
		int swId=schedulWeekBean.getSwId();
		Date thruDate = schedulWeekBean.getThruDate();
		
		SchedulWeek schedulWeekDB=schedulWeekDao.selectByPrimaryKey(swId);
		//判断结束日期，可延长，不可缩短
		Date thruDateDB = schedulWeekDB.getThruDate();
		Date fromDate = schedulWeekDB.getFromDate();
		if (thruDateDB.after(thruDate)) {
			throw new ResultFaultException("5_00005");
		} else if (thruDateDB.before(thruDate)){//如果天数延长，重新生成班次天表信息
			//判断班次类型
			if (swType == 1) {
				/**
				 * 先删除，再添加  班次天表
				 */
				SchedulDayExample schedulDayExampleDel=new SchedulDayExample();
				schedulDayExampleDel.createCriteria().andSwIdEqualTo(swId);
				int record=schedulDayDao.deleteByExample(schedulDayExampleDel);
				
				int week0 = schedulWeekBean.getSwSunday();
				int week1 = schedulWeekBean.getSwMonday();
				int week2 = schedulWeekBean.getSwTuesday();
				int week3 = schedulWeekBean.getSwWednesday();
				int week4 = schedulWeekBean.getSwThursday();
				int week5 = schedulWeekBean.getSwFriday();
				int week6 = schedulWeekBean.getSwSaturday();
				
				schedulWeek.setSwMonday(schedulWeekBean.getSwMonday());
				schedulWeek.setSwTuesday(schedulWeekBean.getSwTuesday());
				schedulWeek.setSwWednesday(schedulWeekBean.getSwWednesday());
				schedulWeek.setSwThursday(schedulWeekBean.getSwThursday());
				schedulWeek.setSwFriday(schedulWeekBean.getSwFriday());
				schedulWeek.setSwSaturday(schedulWeekBean.getSwSaturday());
				schedulWeek.setSwSunday(schedulWeekBean.getSwSunday());
				schedulWeek.setIsDefault(schedulWeekBean.getIsDefault());
				schedulWeek.setIsHoliday(schedulWeekBean.getIsHoliday());

				//周班次星期几上班与否的list<boolean>
				List<Boolean> weekBooleanList = new ArrayList<>();
				if (week0==1){//星期日，index=0
					weekBooleanList.add(true);
				} else{
					weekBooleanList.add(false);
				}
				if (week1==1){
					weekBooleanList.add(true);
				} else{
					weekBooleanList.add(false);
				}
				if (week2==1){
					weekBooleanList.add(true);
				} else{
					weekBooleanList.add(false);
				}
				if (week3==1){
					weekBooleanList.add(true);
				} else{
					weekBooleanList.add(false);
				}
				if (week4==1){
					weekBooleanList.add(true);
				} else{
					weekBooleanList.add(false);
				}
				if (week5==1){
					weekBooleanList.add(true);
				} else{
					weekBooleanList.add(false);
				}
				if (week6==1){
					weekBooleanList.add(true);
				} else{
					weekBooleanList.add(false);
				}
				
				//星期计算
				Calendar calWeek = Calendar.getInstance();  
				calWeek.setTime(fromDate);
				int dayForWeek = calWeek.get(Calendar.DAY_OF_WEEK) - 1;//星期日是0....
				//两个日期循环
				Long startTime = fromDate.getTime();
				Long endTime = thruDate.getTime();
				
				Long oneDay = 1000 * 60 * 60 * 24l;  
				
				schedulDayKey.setSwId(swId);
				while (startTime <= endTime) {
					int index = dayForWeek % 7;//循环下标的布尔值
					Date sdDate = new Date(startTime);
					if (weekBooleanList.get(index)){
						schedulDayKey.setSdDate(sdDate);
						record = schedulDayDao.insert(schedulDayKey);
						if (record<=0) {
							return false;
						}
					}
					dayForWeek++;
					startTime += oneDay;
				}
				//判断is Holiday
				if (schedulWeekBean.getIsHoliday()==1){
					//查询某年的节假日
					HolidayExample holidayExample=new HolidayExample();
					int year = calWeek.get(Calendar.YEAR);
					holidayExample.createCriteria().andHolidayYearEqualTo(year);//
					List<Holiday> holidayList = holidayDao.selectByExample(holidayExample);
					//删除再添加，两步走
					SchedulDayKey sdKey=new SchedulDayKey();
					sdKey.setSwId(swId);
					for (Holiday holiday : holidayList){
						Date holidayDate=holiday.getHolidayDate();
						sdKey.setSdDate(holidayDate);
						record=schedulDayDao.deleteByPrimaryKey(sdKey);
						schedulDayKey.setSdDate(holidayDate);
						if (holiday.getIsToWork()==1){
							schedulDayDao.insertSelective(schedulDayKey);
						}
					}
				}
			}
		}
		
		schedulWeek.setSwId(swId);
		schedulWeek.setSwName(schedulWeekBean.getSwName());
		//修改班次设置
		int record=schedulWeekDao.updateByPrimaryKeySelective(schedulWeek);
		
		//新提交的人员ID集合
		List<Integer> employeeIdList=schedulWeekBean.getEmployeeIdList();
		if (employeeIdList.size()>0) {
			//更新原此班人员排班，人员未变的
//			EmployeeSchedulExample example2=new EmployeeSchedulExample();
			//在employeeIdList里的；班次ID==swId的
//			example2.createCriteria().andEmployeeIdIn(employeeIdList)
//			.andSwIdEqualTo(swId);
//			EmployeeSchedul employeeSchedul2=new EmployeeSchedul();
//			employeeSchedul2.setPunchTimeId(schedulWeekBean.getPunchTimeId());
//			employeeSchedul2.setThruDate(schedulWeekBean.getThruDate());
//			employeeSchedulDao.updateByExampleSelective(employeeSchedul2, example2);

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(schedulWeekBean.getFromDate());
			calendar.add(Calendar.DATE, -1);
			Date thruDateFront = calendar.getTime();//本次排班开始时间的前一天
			/*
			 * 原排班的结束日期大于本次排班的开始日期，并且原排班的开始日期小于本次排班的结束日期，就是有交集！
			 */
			//查出默认班次ID，修改时排除默认班次
			SchedulWeekExample schedulWeekExample=new SchedulWeekExample();
			schedulWeekExample.createCriteria().andIsDefaultEqualTo(1);
			List<SchedulWeek> schedulWeekList=schedulWeekDao.selectByExample(schedulWeekExample);
			Integer defaultId=schedulWeekList.get(0).getSwId();
			//更新有交集的排班（除本班次）,设置人员上个排班结束时间,为本次排班的开始日期的前一天
			EmployeeSchedulExample employeeSchedulExample=new EmployeeSchedulExample();
			employeeSchedulExample.createCriteria().andFromDateLessThanOrEqualTo(thruDate)
			.andThruDateGreaterThanOrEqualTo(schedulWeekBean.getFromDate())
			.andEmployeeIdIn(employeeIdList)
			.andSwIdNotEqualTo(defaultId)
			.andSwIdNotEqualTo(swId);
			EmployeeSchedul employeeSchedul=new EmployeeSchedul();
			employeeSchedul.setThruDate(thruDateFront);
			employeeSchedulDao.updateByExampleSelective(employeeSchedul, employeeSchedulExample);
			
			//减少人员,获取数据库DB中的在排班人员集合
			List<Integer> employeeIdListDB=schedulWeekExtDao.getEmployeeListBySwId(swId);
			if (employeeIdListDB.size()>0) {
				EmployeeSchedulExample example3=new EmployeeSchedulExample();
				example3.createCriteria().andSwIdEqualTo(swId)
				.andEmployeeIdIn(employeeIdListDB)
				.andEmployeeIdNotIn(employeeIdList);
				employeeSchedulDao.deleteByExample(example3);
			}
			//查询班次关系表集合
			EmployeeSchedulExample employeeSchedulExample2=new EmployeeSchedulExample();
			employeeSchedulExample2.createCriteria().andSwIdEqualTo(swId)
			.andThruDateGreaterThanOrEqualTo(fromDate);
			List<EmployeeSchedul> employeeSchedulList=employeeSchedulDao.selectByExample(employeeSchedulExample2);
			//循环判断是否与新增的员工，新增排班
			EmployeeSchedul employeeSchedul4=new EmployeeSchedul();
			employeeSchedul4.setSwId(swId);
			employeeSchedul4.setPunchTimeId(schedulWeekBean.getPunchTimeId());
			employeeSchedul4.setFromDate(schedulWeekBean.getFromDate());
			employeeSchedul4.setThruDate(schedulWeekBean.getThruDate());
			for(int i=0;i<employeeIdList.size();i++){//新的IdList
				boolean type=true;
				for(int j=0;j<employeeSchedulList.size();j++){
					if(employeeIdList.get(i)==employeeSchedulList.get(j).getEmployeeId()){
						type=false;
						break;
					}
				}
				if(type){//TODO
					//新增人员排班 
					List<EmployeePunchTimeBean> epList=schedulWeekBean.getEmployeePunchTimeList();
					if (epList == null || epList.size() == 0) {//正常情况
						employeeSchedul4.setEmployeeId(employeeIdList.get(i));
						record=employeeSchedulDao.insertSelective(employeeSchedul4);
						if (record<=0) {
							return false;
						}
					} else {//每天不同打卡时间的排班，循环按天增加班次
						for (EmployeePunchTimeBean employeePunchTimeBean : epList) {
							employeeSchedul4=new EmployeeSchedul();
							employeeSchedul4.setSwId(swId);
							employeeSchedul4.setPunchTimeId(employeePunchTimeBean.getPunchTimeId());
							employeeSchedul4.setFromDate(employeePunchTimeBean.getDate());
							employeeSchedul4.setThruDate(employeePunchTimeBean.getDate());
							employeeSchedul4.setEmployeeId(employeeIdList.get(i));
							if(employeeSchedul4.getPunchTimeId()==null){
								employeeSchedul4.setPunchTimeId(1);
							}
							record=employeeSchedulDao.insertSelective(employeeSchedul4);
							if (record<=0) {
								return false;
							}
						}
					}
				}
			}
		} else {//删除了原来的（DB里）所有人的排班employeeSchedul
			//减少人员,获取数据库DB中的在排班人员集合
			List<Integer> employeeIdListDB=schedulWeekExtDao.getEmployeeListBySwId(swId);
			if (employeeIdListDB.size()>0) {
				EmployeeSchedulExample example3=new EmployeeSchedulExample();
				example3.createCriteria().andSwIdEqualTo(swId)
				.andEmployeeIdIn(employeeIdListDB)
				.andEmployeeIdNotIn(employeeIdList);
				employeeSchedulDao.deleteByExample(example3);
			}
		}
		return true;
	}

	/**
	 * 查询班次
	 */
	@Override
	public List<SchedulWeekBean> getSchedulWeekList() {
		List<SchedulWeekBean> schedulWeekBeanList=schedulWeekExtDao.getSchedulWeekList(new Date());
		for(int i=0;i<schedulWeekBeanList.size();i++){
			//查询班次人员数量
			Integer employeeSum=schedulWeekExtDao.getEmployeeIdCountBySwId(schedulWeekBeanList.get(i).getSwId());
			schedulWeekBeanList.get(i).setEmployeeSum(employeeSum);
			//查询班次打卡时间数量
			List<Integer> punchTimeIdList=schedulWeekExtDao.getPunchTimeIdListBySwId(schedulWeekBeanList.get(i).getSwId());
			if(punchTimeIdList.size()<=1){
				PunchTime punchTime=punchTimeDao.selectByPrimaryKey(punchTimeIdList.get(0));
				if(punchTime!=null){
					schedulWeekBeanList.get(i).setPunchTimeId(punchTime.getPunchTimeId());
					schedulWeekBeanList.get(i).setStartFromTime(punchTime.getStartFromTime());
					schedulWeekBeanList.get(i).setStartToTime(punchTime.getStartToTime());
					schedulWeekBeanList.get(i).setStartInterval(punchTime.getStartInterval());
					schedulWeekBeanList.get(i).setEndFromTime(punchTime.getEndFromTime());
					schedulWeekBeanList.get(i).setEndToTime(punchTime.getEndToTime());
					schedulWeekBeanList.get(i).setEndInterval(punchTime.getEndInterval());
				}
			}
			
			List<SchedulWeekBean> punchTimeIdDateList=schedulWeekExtDao.getDatePunchTimeIdListBySwId(schedulWeekBeanList.get(i).getSwId());
			List<EmployeePunchTimeBean> employeePunchTimeList=new ArrayList<EmployeePunchTimeBean>();
			if(punchTimeIdDateList.size()>0){
				for(int j=0;j<punchTimeIdDateList.size();j++){
					EmployeePunchTimeBean employeePunchTimeBean=new EmployeePunchTimeBean();
					employeePunchTimeBean.setDate(punchTimeIdDateList.get(j).getFromDate());
					employeePunchTimeBean.setPunchTimeId(punchTimeIdDateList.get(j).getPunchTimeId());
					employeePunchTimeList.add(employeePunchTimeBean);
				}
				schedulWeekBeanList.get(i).setEmployeePunchTimeList(employeePunchTimeList);
			}
		}
		return schedulWeekBeanList;
	}

	/**
	 * 查询当前班次by班次ID,还有swType
	 */
	@Override
	public SchedulWeekBean getSchedulWeekBySwId(String swIdStr) {
		
		int swId=Integer.parseInt(swIdStr);
		SchedulWeekBean schedulWeekBean=new SchedulWeekBean();
		SchedulWeek schedulWeek=schedulWeekDao.selectByPrimaryKey(swId);
		int swType=schedulWeek.getSwType();
		if (swType==1) {//周班次
			schedulWeekBean.setSwMonday(schedulWeek.getSwMonday());
			schedulWeekBean.setSwTuesday(schedulWeek.getSwTuesday());
			schedulWeekBean.setSwWednesday(schedulWeek.getSwWednesday());
			schedulWeekBean.setSwThursday(schedulWeek.getSwThursday());
			schedulWeekBean.setSwFriday(schedulWeek.getSwFriday());
			schedulWeekBean.setSwSaturday(schedulWeek.getSwSaturday());
			schedulWeekBean.setSwSunday(schedulWeek.getSwSunday());
			schedulWeekBean.setIsDefault(schedulWeek.getIsDefault());
			schedulWeekBean.setIsHoliday(schedulWeek.getIsHoliday());
		}
		if (swType==2) {//月班次
			SchedulDayExample example=new SchedulDayExample();
			example.createCriteria().andSwIdEqualTo(swId);
			List<SchedulDayKey> schedulDayKeyList = schedulDayDao.selectByExample(example);
			List<Date> dateList=new ArrayList<>();
			for (SchedulDayKey schedulDayKey : schedulDayKeyList) {
				dateList.add(schedulDayKey.getSdDate());
			}
			schedulWeekBean.setDateList(dateList);
		}
		
		schedulWeekBean.setSwName(schedulWeek.getSwName());
		schedulWeekBean.setFromDate(schedulWeek.getFromDate());
		schedulWeekBean.setThruDate(schedulWeek.getThruDate());
		schedulWeekBean.setSwType(swType);
		List<Integer> employeeList = schedulWeekExtDao.getEmployeeListBySwId(swId);
		schedulWeekBean.setEmployeeIdList(employeeList);
		
		List<SchedulWeekBean> punchTimeIdDateList=schedulWeekExtDao.getDatePunchTimeIdListBySwId(swId);
		List<EmployeePunchTimeBean> employeePunchTimeList=new ArrayList<EmployeePunchTimeBean>();
		if(punchTimeIdDateList.size()>0){
			for(int i=0;i<punchTimeIdDateList.size();i++){
				EmployeePunchTimeBean employeePunchTimeBean=new EmployeePunchTimeBean();
				employeePunchTimeBean.setDate(punchTimeIdDateList.get(i).getFromDate());
				employeePunchTimeBean.setPunchTimeId(punchTimeIdDateList.get(i).getPunchTimeId());
				employeePunchTimeList.add(employeePunchTimeBean);
			}
			schedulWeekBean.setEmployeePunchTimeList(employeePunchTimeList);
		}
		
		return schedulWeekBean;
	}
	
	/**
	 * 添加验证标题是否存在(先不用，判断可能不全)
	 */
	@Override
	public boolean getSaveSchedulWeekValidate(SchedulWeekBean schedulWeekBean) {
		SimpleDateFormat dateformat=new SimpleDateFormat("yyyy/MM/dd");
		errorMsgList=new ArrayList<String>();
		SchedulWeekExample schedulWeekExample=new SchedulWeekExample();
		SchedulWeekExample.Criteria schedulWeekCriter=schedulWeekExample.createCriteria();
		schedulWeekCriter.andSwNameEqualTo(schedulWeekBean.getSwName());
		schedulWeekCriter.andIsDeleteEqualTo(0);
		if (schedulWeekBean.getSwId()!=null) {
			schedulWeekCriter.andSwIdNotEqualTo(schedulWeekBean.getSwId());
		}
		//一.查询班次名称是否重复
		List<SchedulWeek> schedulWeekList=schedulWeekDao.selectByExample(schedulWeekExample);
		if (schedulWeekList.size()>0) {
			throw new ResultFaultException("5_00001");
		}
		//二.验证用户等开始日期是否大于，新增的开始日期
		for(int i=0;i<schedulWeekBean.getEmployeeIdList().size();i++){
			EmployeeSchedulExample example=new EmployeeSchedulExample();
			example.createCriteria().andEmployeeIdEqualTo(schedulWeekBean.getEmployeeIdList().get(i))
			.andFromDateGreaterThanOrEqualTo(schedulWeekBean.getFromDate()).andIsDeleteEqualTo(0);
			example.setOrderByClause("from_date desc");
			List<EmployeeSchedul> employeeSchedulList=employeeSchedulDao.selectByExample(example);
			if(employeeSchedulList.size()>0){
				Employee employee=employeeDao.selectByPrimaryKey(schedulWeekBean.getEmployeeIdList().get(i));
				if(employee!=null){
					String errorStr="("+employee.getEmployeeNo()+")"+employee.getChineseName()+"-开始日期："+dateformat.format(employeeSchedulList.get(0).getFromDate());
					errorMsgList.add(errorStr);
				}
			}
		}
		if(errorMsgList.size()>0){
			throw new ResultFaultException("5_00003", errorMsgList);
		}
		return true;
	}

	/**
	 * 修改验证是否存在(先不用，判断可能不全，也有日期不能改的变化)
	 */
	@Override
	public boolean getUpdateSchedulWeekValidate(SchedulWeekBean schedulWeekBean) {
		SimpleDateFormat dateformat=new SimpleDateFormat("yyyy/MM/dd");
		errorMsgList=new ArrayList<String>();
		SchedulWeek schedulWeek=schedulWeekDao.selectByPrimaryKey(schedulWeekBean.getSwId());
		if (schedulWeek==null) {
			throw new ResultFaultException("0_00012");
		}
		
		SchedulWeekExample schedulWeekExample=new SchedulWeekExample();
		SchedulWeekExample.Criteria schedulWeekCriter=schedulWeekExample.createCriteria();
		schedulWeekCriter.andSwNameEqualTo(schedulWeekBean.getSwName());
		if (schedulWeekBean.getSwId()!=null) {
			schedulWeekCriter.andSwIdNotEqualTo(schedulWeekBean.getSwId());
		}
		//一.查询班次名称是否重复
		List<SchedulWeek> schedulWeekList=schedulWeekDao.selectByExample(schedulWeekExample);
		if (schedulWeekList.size()>0) {
			throw new ResultFaultException("5_00001");
		}
		
		//判断如果开始日期和结束日期有变更
		if(!schedulWeek.getFromDate().equals(schedulWeekBean.getFromDate())){
			//二.验证用户等开始日期是否大于，新增的开始日期
			for(int i=0;i<schedulWeekBean.getEmployeeIdList().size();i++){
				EmployeeSchedulExample example=new EmployeeSchedulExample();
				example.createCriteria().andEmployeeIdEqualTo(schedulWeekBean.getEmployeeIdList().get(i))
				.andFromDateGreaterThanOrEqualTo(schedulWeekBean.getFromDate())
				.andIsDeleteEqualTo(0)
				.andSwIdNotEqualTo(schedulWeekBean.getSwId());
				example.setOrderByClause("from_date desc");
				List<EmployeeSchedul> employeeSchedulList=employeeSchedulDao.selectByExample(example);
				if(employeeSchedulList.size()>0){
					Employee employee=employeeDao.selectByPrimaryKey(schedulWeekBean.getEmployeeIdList().get(i));
					if(employee!=null){
						String errorStr="("+employee.getEmployeeNo()+")"+employee.getChineseName()+"-开始日期："+dateformat.format(employeeSchedulList.get(0).getFromDate());
						errorMsgList.add(errorStr);
					}
				}
			}
			if(errorMsgList.size()>0){
				throw new ResultFaultException("5_00003", errorMsgList);
			}
		}
		return true;
	}
	/**
	 * 删除验证是否存在
	 */
	@Override
	public boolean getDeleteSchedulWeekValidate(String id) {
		SchedulWeek schedulWeek=schedulWeekDao.selectByPrimaryKey(Integer.parseInt(id));
		if (schedulWeek==null) {
			throw new ResultFaultException("0_00012");
		}
		return true;
	}

	/**
	 * 验证是否排班时间有重叠
	 */
	@Override
	public boolean getVerifySchedulWeek(SchedulWeekBean schedulWeekBean) {
		List<Integer> employeeIdList=schedulWeekBean.getEmployeeIdList();
		if (employeeIdList.size()>0) {
			EmployeeSchedulExample example=new EmployeeSchedulExample();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(schedulWeekBean.getFromDate());
			calendar.add(Calendar.DATE, -1);
			Date thruDate = calendar.getTime();
			//原排班的结束日期大于本次排班的开始日期前一日//原排班的开始日期小于本次的结束日期，//且除去本排班swId的//更新，员工ID在list内的，，就是有重叠
			EmployeeSchedulExample.Criteria cri=example.createCriteria();
			cri.andThruDateGreaterThan(thruDate).andEmployeeIdIn(employeeIdList);
			if (schedulWeekBean.getIsDefault()!=1) {
				cri.andFromDateLessThan(schedulWeekBean.getThruDate());
			}
			//新增没swID，修改有swId
			if (schedulWeekBean.getSwId()!=null) {
				cri.andSwIdNotEqualTo(schedulWeekBean.getSwId());
			}
			if (employeeSchedulDao.selectByExample(example).size()>0) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 新增（自动添加）周班次天数据
	 * 只是默认班次
	 */
	@Override
	public boolean saveSchedulDayKeyAuto() {
		Date nowDate=new Date();
		SchedulWeekExample schedulWeekExample=new SchedulWeekExample();
		schedulWeekExample.createCriteria().andIsDefaultEqualTo(1);
		List<SchedulWeek> schedulWeeklist=schedulWeekDao.selectByExample(schedulWeekExample);
		List<Integer> swIdList=new ArrayList<Integer>();
		for(int si=0;si<schedulWeeklist.size();si++) {
			swIdList.add(schedulWeeklist.get(si).getSwId());
		}
		
		//删除已经生成班车数据
		if(swIdList.size()>0) {
			//计算下个月月初和月末时间
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(nowDate);
			//calendar.add(Calendar.MONTH, 1);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			Date monthStartDate=calendar.getTime();//月初时间
			calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

			//月末
			Calendar ofCalendar = Calendar.getInstance();
			ofCalendar.setTime(nowDate);
			ofCalendar.add(Calendar.MONTH, 6);
			ofCalendar.set(Calendar.DAY_OF_MONTH, 1);
			Date ofMonthStartDate=ofCalendar.getTime();//月初时间
			ofCalendar.set(Calendar.DAY_OF_MONTH, ofCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
			Date ofMonthEndDate=ofCalendar.getTime();//月末时间
			
			SchedulDayExample schedulDayExample=new SchedulDayExample();
			schedulDayExample.createCriteria().andSwIdIn(swIdList).andSdDateBetween(monthStartDate, ofMonthEndDate);
			int record=schedulDayDao.countByExample(schedulDayExample);
			if(record>0) {
				schedulDayDao.deleteByExample(schedulDayExample);
			}
		}
		
		//六个月版本
		for(int i=0;i<7;i++) {
			//计算下个月月初和月末时间
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(nowDate);
			calendar.add(Calendar.MONTH, i);
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			Date monthStartDate=calendar.getTime();//月初时间
			calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
			Date monthEndDate=calendar.getTime();//月末时间
			//生成班车
			this.setSchedulDay(schedulWeeklist, monthStartDate, monthEndDate);
		}
		return true;
	}
	
	/**
	 * 生成班车
	 * @param schedulWeeklist
	 * @param monthStartDate
	 * @param monthEndDate
	 * @return
	 */
	private void setSchedulDay(List<SchedulWeek> schedulWeeklist,Date monthStartDate,Date monthEndDate) {
		//循环各个周班次(现在仅有默认班次)
		for (SchedulWeek schedulWeek : schedulWeeklist) {
			int swId=schedulWeek.getSwId();
			int record;
			//周班次星期几上班与否的list<boolean>
			int week0 = schedulWeek.getSwSunday();
			int week1 = schedulWeek.getSwMonday();
			int week2 = schedulWeek.getSwTuesday();
			int week3 = schedulWeek.getSwWednesday();
			int week4 = schedulWeek.getSwThursday();
			int week5 = schedulWeek.getSwFriday();
			int week6 = schedulWeek.getSwSaturday();
			
			List<Boolean> weekBooleanList = new ArrayList<>();
			if (week0==1){//星期日，index=0
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			if (week1==1){
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			if (week2==1){
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			if (week3==1){
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			if (week4==1){
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			if (week5==1){
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			if (week6==1){
				weekBooleanList.add(true);
			} else{
				weekBooleanList.add(false);
			}
			
			//星期计算
			Calendar calWeek = Calendar.getInstance();  
			calWeek.setTime(monthStartDate);
			int dayForWeek = calWeek.get(Calendar.DAY_OF_WEEK) - 1;//星期日是0....
			
			/**
			 * 两个日期循环,自动新增班次天表
			 */
			Long startTime = monthStartDate.getTime();
			Long endTime = monthEndDate.getTime();
			Long oneDay = 1000 * 60 * 60 * 24l;  
			
			SchedulDayKey schedulDayKey=new SchedulDayKey();//班次天表(双主键)
			schedulDayKey.setSwId(swId);
			while (startTime <= endTime) {
				int index = dayForWeek % 7;//循环下标的布尔值
				Date sdDate = new Date(startTime);
				if (weekBooleanList.get(index)){
					schedulDayKey.setSdDate(sdDate);
					SchedulDayExample schedulDayExample=new SchedulDayExample();
					schedulDayExample.createCriteria()
					.andSwIdEqualTo(swId)
					.andSdDateEqualTo(sdDate);
					record=schedulDayDao.countByExample(schedulDayExample);
					if(record<=0) {
						schedulDayDao.insert(schedulDayKey);
					}
				}
				dayForWeek++;
				startTime += oneDay;
			}
			
			//判断is Holiday
			if (schedulWeek.getIsHoliday()==1){
				//查询某年的节假日
				HolidayExample holidayExample=new HolidayExample();
				int year = calWeek.get(Calendar.YEAR);
				holidayExample.createCriteria().andHolidayYearEqualTo(year);
				List<Holiday> holidayList = holidayDao.selectByExample(holidayExample);
				//删除再添加，两步走
				SchedulDayKey sdKey=new SchedulDayKey();
				sdKey.setSwId(swId);
				for (Holiday holiday : holidayList){
					Date holidayDate=holiday.getHolidayDate();
					sdKey.setSdDate(holidayDate);
					record=schedulDayDao.deleteByPrimaryKey(sdKey);
					schedulDayKey.setSdDate(holidayDate);
					if (holiday.getIsToWork()==1){
						schedulDayDao.insertSelective(schedulDayKey);
					}
				}
			}
		}
	}

	/**
	 * 导入特殊排班
	 */
	@Override
	public List<String> saveSchedulWeekExcel(Integer attachmentId) {
		// TODO Auto-generated method stub
		List<String> successList = new ArrayList<String>();
		// 查询附件表获取路径地址
		Attachment attachment = attachmentDao.selectByPrimaryKey(attachmentId);
		// 设置文件路径
		String pathName = attachment.getUrl().replace(AppConstant.UPLOAD_RESOURCE, AppConstant.SAVE_FILE_REPOSITORY);
		File file = new File(pathName);
		Workbook work = null;
		if (file.exists()) {
			try {
				InputStream inputStream = new FileInputStream(file);
				if (pathName.matches("^.+\\.(?i)(xls)$")) {
					work = new HSSFWorkbook(inputStream);
				} else if (pathName.matches("^.+\\.(?i)(xlsx)$")) {
					work = new XSSFWorkbook(inputStream);
				}
			} catch (Exception ex) {
				throw new ResultFaultException("0_00003");
			}
		} else {
			throw new ResultFaultException("0_00003");
		}
		if (work == null) {
			throw new ResultFaultException("0_00003");
		}

		if (work != null) {
			successList = addSchedulWeek(work,successList);
//			successList.addAll(schedulWeekList);
		}
		return successList;
	}
	/**
	 * 导入excel表格，逐行读取操作
	 * @param work12
	 * @param successList
	 * @return
	 */
	private List<String> addSchedulWeek(Workbook work,List<String> successList) {
		//取打卡时间Map
		PunchTimeExample ptExample=new PunchTimeExample();
		ptExample.createCriteria().andIsDeleteEqualTo(0);//0未删除的
		List<PunchTime> ptList = punchTimeDao.selectByExample(ptExample);
		Map<String, Integer> ptMap = new HashMap<>();
		//取得默认打卡时间名称
		String defaultPTName = null;
		for (PunchTime punchTime : ptList) {
			ptMap.put(punchTime.getPunchTimeName(), punchTime.getPunchTimeId());
			if (punchTime.getIsDefault()==1) {
				defaultPTName = punchTime.getPunchTimeName();
			}
		}
		// 查询员工
		List<Employee> employeeList = employeeExtDao.getEmployeeList(new Date(), new Date());
		Map<String, Integer> employeeMap = new HashMap<String, Integer>();
		Map<Integer, Employee> employeeIdMap = new HashMap<Integer, Employee>();
		for (int ei = 0; ei < employeeList.size(); ei++) {
			employeeMap.put(employeeList.get(ei).getEmployeeNo(), employeeList.get(ei).getEmployeeId());
			employeeIdMap.put(employeeList.get(ei).getEmployeeId(), employeeList.get(ei));
		}

		// 获取工作表的数量
		int sheetNum = work.getNumberOfSheets();
		// 遍历工作表 找出要导入的Excel表
		for (int m = 0; m < sheetNum; m++) {
			Sheet sh = work.getSheetAt(m);
			if (sh.getSheetName().equals("排班信息")) {
				Sheet sheet = work.getSheetAt(m);
				List<String> errorMsgList = new ArrayList<String>();// 创建错误信息列表
				
				//排班Bean 
				SchedulWeekBean schedulWeekBean = new SchedulWeekBean();
				//排班人员id List
				List<Integer> employeeIdList = new ArrayList<>();
				//实际上班的日期
				List<Date> dateList = new ArrayList<>();
				//排班日期（上班和休息的），打卡id的List
				List<EmployeePunchTimeBean> eptList=new ArrayList<>();
				
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");// 可以方便地修改日期格式
				String employeeNo = null;
				String employeeNoTemp = null;//比较用
				// 扫描Excel每行的每个单元格
				for (int i = 1; i <= sheet.getLastRowNum()+1; i++) {
					StringBuilder str = new StringBuilder("第" + (i + 1) + "行");
					Row row = sheet.getRow(i);
					// 如果一整行为空则结束循环
					if (row == null || i == sheet.getLastRowNum()+1) {
						this.saveBeforeBreak(schedulWeekBean, employeeIdList, dateList, eptList);
						break;
					}
					//遍历必填项列，返回错误信息
					boolean isNull = false;//如果一行有一个格为Null
					boolean isAllNull = true;//如果所有行都为Null，就break
					for (int c = 0; c < 6; c++) {
						if (row.getCell(c) == null || StringUtils.isBlank(row.getCell(c).toString())) {
							if (c!=1 && c!=3) {//姓名可以不填，打卡名称休息日可不填
								str.append("第" + (c + 1) + "列，不能为空。");
								isNull = true;
							}
						} else {
							isAllNull = false;
						}
					}
					if (isNull) {
						if (isAllNull) {//此行都为空值，break结束循环
							this.saveBeforeBreak(schedulWeekBean, employeeIdList, dateList, eptList);
							break;
						}
						errorMsgList.add(str.toString());
						continue;
					}

					// 判断是否为数字
					if (row.getCell(0).getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
						employeeNo = new BigDecimal(row.getCell(0).getNumericCellValue()).intValue() + "";
					} else {
						employeeNo = row.getCell(0).toString();
					}
//					if (employeeNo.length() <= 4) {
//						employeeNo = "0" + employeeNo;
//					}
					// 判断员工号是否存在
					if (employeeMap.containsKey(employeeNo)) {
						//判断是否是同一个人的employeeNo
						if (employeeNoTemp==null || !employeeNoTemp.equals(employeeNo)) {//不是同一个人，新new一个排班信息Bean
							this.saveBeforeBreak(schedulWeekBean, employeeIdList, dateList, eptList);
							
							schedulWeekBean = new SchedulWeekBean();
							employeeIdList = new ArrayList<>();
							//实际上班的日期
							dateList = new ArrayList<>();
							eptList = new ArrayList<>();
							//保存员工ID
							employeeIdList.add(employeeMap.get(employeeNo));
							
							employeeNoTemp = employeeNo;
							//保存 第六列，班次名称
							schedulWeekBean.setSwName(row.getCell(5).toString());
							//保存为特殊排班
							schedulWeekBean.setSwType(2);
							//不按法定假
							schedulWeekBean.setIsHoliday(0);
						}
						Date swDate = new Date();
						// 判断是否为字符串或是日期类型
						try {
							// 判断是否为字符串
							if (row.getCell(2).getCellType() == HSSFCell.CELL_TYPE_STRING) {
								swDate = dateFormat.parse(row.getCell(2).toString());
							}
							// 判断是否为日期
							else if (HSSFDateUtil.isCellDateFormatted(row.getCell(2))) {
								swDate = row.getCell(2).getDateCellValue();
							}
							if (schedulWeekBean.getFromDate()==null) {
								schedulWeekBean.setFromDate(swDate);
							}
							schedulWeekBean.setThruDate(swDate);
							//记录日期和打卡时间id（上班和休息的）
							EmployeePunchTimeBean ept=new EmployeePunchTimeBean();
							ept.setDate(swDate);
							//第五列，是否上班
							if (row.getCell(4).toString().equals("上班")) {
								dateList.add(swDate);
								//第四列打卡时间名称
								if (ptMap.containsKey(row.getCell(3).toString())) {
									//保存打卡时间id
									ept.setPunchTimeId(ptMap.get(row.getCell(3).toString()));
								} else {
									str.append("打卡时间不存在,");
								}
							} else {
								//第四列打卡时间名称
								if (ptMap.containsKey(row.getCell(3).toString())) {
									//保存打卡时间id
									ept.setPunchTimeId(ptMap.get(row.getCell(3).toString()));
								} else {
									//打卡时间为空时，保存为默认打卡时间id
									ept.setPunchTimeId(ptMap.get(defaultPTName));
								}
							}
							eptList.add(ept);
							
						} catch (Exception e) {
							str.append("日期格式不正确,");
						}
					} else {
						str.append("员工号不存在,");
					}
					// 判断错误信息长度
					if (str.length() == new String("第" + (i + 1) + "行").length()) {
					} else {
						String errorStr = str.toString().substring(0, str.toString().length() - 1);
						errorMsgList.add(errorStr);
					}
				}
				if (errorMsgList.size() > 0) {
					throw new ResultFaultException("8_00002", errorMsgList);
				}
				successList.add(0, "考勤信息导入完成。");
				return successList;
			}
		}
		throw new ResultFaultException("0_00014");
	}
	/**
	 * 结束一个人的循环前，保存特殊排班
	 * @param schedulWeekBean  排班Bean
	 * @param employeeIdList  排班人员id List
	 * @param dateList  实际上班的日期
	 * @param eptList  排班日期（上班和休息的），打卡id的List
	 */
	private void saveBeforeBreak(SchedulWeekBean schedulWeekBean,List<Integer> employeeIdList,List<Date> dateList
			,List<EmployeePunchTimeBean> eptList) {
		//保存前一个人的排班信息
		if (schedulWeekBean.getSwName() != null) {//验证是已经保存的信息
			schedulWeekBean.setDateList(dateList);
			schedulWeekBean.setEmployeeIdList(employeeIdList);
			schedulWeekBean.setEmployeePunchTimeList(eptList);
			this.saveSchedulWeek(schedulWeekBean);
			//重计算
			approvalService.resetAttendance(schedulWeekBean.getFromDate(), schedulWeekBean.getThruDate(), employeeIdList);
		}
	}
}
